package cn.itlight.controller;


import cn.itlight.basic.resultBase;
import cn.itlight.domain.Product;
import cn.itlight.domain.Store;
import cn.itlight.feign.ProductFeignClient;
import cn.itlight.service.IProductService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private ProductFeignClient productFeignClient;

    @PostMapping("/add")
    @HystrixCommand(fallbackMethod = "fallbackMethod")
    public resultBase add(@RequestBody Product product) {
        try {
            productService.add(product);
            System.out.println(product);
            Store store = new Store();
            store.setProduct_id(product.getId());
            store.setNumber(product.getNumber());
            productFeignClient.add(store);
            resultBase resultBase = new resultBase();
            resultBase.setSuccess(true);
            resultBase.setMessage("添加成功");

            return resultBase;
        }catch (Exception e){
            e.printStackTrace();
            resultBase resultBase = new resultBase();
            resultBase.setSuccess(false);
            resultBase.setMessage("失败请重试");
            return resultBase;
        }


    }

    public resultBase fallbackMethod(@RequestBody Product product) {

        resultBase resultBase = new resultBase();
        resultBase.setSuccess(false);
        resultBase.setMessage("失败请重试");

        return resultBase;
    }

}

